<?php
namespace app\common\model;

use think\Model;

class BaseModel extends Model
{
    
    public function getAll()
    {
        return $this->select();
    }

    public function getOne($where)
    {
        if (!is_array($where)) {
            $where = [
                [$this->getPk(), '=', $where]
            ];
        }
        return $this->where($where)->find();
    }

    public function doPage($page = 1, $listRows = 10)
    {
        if (is_null($listRows) && strpos($page, ',')) {
            list($page, $listRows) = explode(',', $page);
        }
        $page = $page <=0 ? 1 : $page;
        $this->limit(($page - 1) * $listRows, $listRows);
        $this->options['page'] = [intval($page), intval($listRows)];
        return $this;
    }

    public function __call($method, $args)
    {

        $allow_methods = ['all', 'get', 'page'];
        $map_methods = ['all' => 'getAll', 'get' => 'getOne', 'page' => 'doPage'];
        if (in_array($method, $allow_methods)) {
            return call_user_func_array([$this, $map_methods[$method]], $args);
        }

        return parent::__call($method, $args);
    }

    public static function __callStatic($method, $args)
    {
        $allow_methods = ['all', 'get', 'page'];
        $map_methods = ['all' => 'getAll', 'get' => 'getOne', 'page' => 'doPage'];
        if (in_array($method, $allow_methods)) {
            return call_user_func_array([new static(), $map_methods[$method]], $args);
        }
        return parent::__callStatic($method, $args);
    }
}